using GameBaseFramework.Base;
using GameBaseFramework.Commands;
using GameBaseFramework.Extension;
using GameBaseFramework.Network;
using GameBaseFramework.Pattern;
using GameGraphics.HomeWorld;
using GameUnityFramework.Resource;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace GameGraphics
{
	public class UIViewChattingRoom : UIViewChattingRoomBase, IPointerDownHandler, IPointerUpHandler, IDragHandler, IPointerClickHandler
	{
        bool IsDraging;

		protected override void OnEnter(object data = null)
		{
			base.OnEnter(data);
			_transformContent.RemoveAllChilds();
            this.BindCommand<GetMessageCommand>(GetMessage);
            GetMessage(new GetMessageCommand() { SenderIP = "192.168.0.0", Content = "加载测试"});
            IsDraging = false;
        }

        /// <summary>
        /// 收到网络消息
        /// </summary>
        /// <param name="obj"></param>
        private void GetMessage(GetMessageCommand command)
        {
            UnityObjectManager.LoadGameObject("TextPrefab", _transformContent, (obj) =>
            {
                obj.GetComponent<Text>().text = command.SenderIP + ":" + command.Content;
                obj.GetComponent<ContentSizeFitter>().ForceUpdateSize();
                _transformContent.GetComponent<ContentSizeFitter>().ForceUpdateSize();
            });
        }

        /// <summary>
        /// 发送
        /// </summary>
        protected override void OnButtonGoClick()
		{
			base.OnButtonGoClick();
            //NetworkManager.Instance.Send(_inputfieldInput.text);
            GetMessage(new GetMessageCommand() { SenderIP = "192.168.0.0", Content = _inputfieldInput.text });
            _inputfieldInput.text = "";
		}

        /// <summary>
        /// 按下
        /// </summary>
        /// <param name="eventData"></param>
        public void OnPointerDown(PointerEventData eventData)
        {
            DebugTool.Log("OnPointerDown");
            IsDraging = true;
        }

        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="eventData"></param>
        public void OnPointerClick(PointerEventData eventData)
        {
            DebugTool.Log("Click");
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            DebugTool.Log("OnPointerUp");
        }


        /// <summary>
        /// 拖动
        /// </summary>
        /// <param name="eventData"></param>
        public void OnDrag(PointerEventData eventData)
        {
            if (IsDraging)
            {
                _transformWindowBg.GetComponent<RectTransform>().anchoredPosition += eventData.delta;
            }
        }
    }
}
